package com.zhaorujie.rpc.core.remoting.invoker;

import com.zhaorujie.rpc.core.RpcProperties;
import com.zhaorujie.rpc.core.register.Register;
import com.zhaorujie.rpc.core.register.RegisterFactory;
import com.zhaorujie.rpc.core.remoting.netty.client.NettyClientConnect;
import com.zhaorujie.rpc.core.remoting.params.RpcFutureResponse;
import com.zhaorujie.rpc.core.remoting.params.RpcResponse;
import com.zhaorujie.rpc.core.serialize.RpcSerialize;
import com.zhaorujie.rpc.core.serialize.SerializeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author 赵汝杰
 * @version v1.0
 * @description RpcInvokerFactory
 * @date 2024/3/8
 **/
public class RpcInvokerFactory {

    private final static Logger logger = LoggerFactory.getLogger(RpcInvokerFactory.class);

    private RpcProperties rpcProperties;

    private RpcSerialize rpcSerialize;

    private Register register;

    private final ConcurrentMap<String, RpcFutureResponse> futureResponsePool = new ConcurrentHashMap<>();

    /**
     * 初始化服务调用者的配置
     * @param rpcProperties
     */
    public void init(RpcProperties rpcProperties) {
        this.rpcProperties = rpcProperties;
        // 获取序列化实例对象
        this.rpcSerialize = SerializeFactory.getInstance(rpcProperties.getSerializeType());
        // 获取注册中心实例对象
        this.register = RegisterFactory.getInstance(rpcProperties.getRegistryType());
        // 初始化注册中心
        register.init(rpcProperties.getRegisterAddr(), rpcProperties.getEnv());

        logger.debug("初始化Invoker factory");
    }

    /**
     * 服务销毁处理
     */
    public void destroy() {
        register.destroy();
        NettyClientConnect.destroy();

        logger.debug("destroy");
    }

    public void setInvokerFuture(String requestId, RpcFutureResponse futureResponse){
        futureResponsePool.put(requestId, futureResponse);
    }

    public void removeInvokerFuture(String requestId){
        futureResponsePool.remove(requestId);
    }

    /**
     * 设置远程调用结果
     * @param requestId 请求id
     * @param rpcResponse 响应结果
     */
    public void notifyInvokerFuture(String requestId, RpcResponse rpcResponse) {
        RpcFutureResponse futureResponse = futureResponsePool.get(requestId);
        if (futureResponse == null) {
            return;
        }
        futureResponse.setRpcResponse(rpcResponse);

        futureResponsePool.remove(requestId);
    }


    public RpcProperties getRpcProperties() {
        return rpcProperties;
    }

    public void setRpcProperties(RpcProperties rpcProperties) {
        this.rpcProperties = rpcProperties;
    }

    public RpcSerialize getRpcSerialize() {
        return rpcSerialize;
    }

    public void setRpcSerialize(RpcSerialize rpcSerialize) {
        this.rpcSerialize = rpcSerialize;
    }

    public Register getRegister() {
        return register;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

}
